package com.ccb.jyzx.expand.tiantouonline.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;

/**
 * Dos命令工具类
 * @projectName: extend
 * @author: jianghaolin
 * @date: 2019/11/21
 * @version: 1.0
 */
public class DosUtil {

    static Logger logger= LoggerFactory.getLogger(DateUtils.class);

    /**
     * 执行dos命令
     * @param cmd
     * @return
     */
    private static boolean excuteDos(String cmd){
        boolean result = false;
        try {
            long start = System.currentTimeMillis();
            logger.info("执行脚本：" + cmd);
            Process pro = null;
            Runtime runTime = Runtime.getRuntime();
            if (runTime == null) {
                logger.info("Create runtime false!");
            }
            try {
                logger.info("开始转换");
                pro = runTime.exec(cmd);
                BufferedReader input = new BufferedReader(new InputStreamReader(pro.getInputStream()));
                PrintWriter output = new PrintWriter(new OutputStreamWriter(pro.getOutputStream()));
                String line;
                while ((line = input.readLine()) != null) {
                    logger.info("line: " + line);
                }
                input.close();
                output.close();
                pro.destroy();
                result = true;
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            long end = System.currentTimeMillis();
            long differ = end - start;
            logger.info("执行dos命令时长：" + differ);
            result = true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }


    /**
     * wordToPdf执行dos命令
     * @param dir word的存放路径
     * @param source word的名字
     * @param target 打成的html的路径
     * @return
     */
    public static boolean runWordToPdf(String dir, String source, String target){
        boolean result = false;
        String cmd = "libreoffice6.1 --convert-to pdf:writer_pdf_Export " +
                dir + source + " --outdir " + target;
        logger.info("执行脚本：" + cmd);
        result = excuteDos(cmd);
        return result;
    }

    /**
     * eft推送
     * @param sourceTag
     * @param target
     * @return
     */
    public static boolean pushTag(String sourceTag, String target){
        logger.info("源文件：" + sourceTag);
        logger.info("目标文件：" + target);
        boolean result = false;
        String cmd = "eft_client -ucfsg 20000001 " + sourceTag + " " + target;
        excuteDos(cmd);
        logger.info("推送成功");
        result = true;
        return result;
    }
}
